package com.biu.billiards.mqqts;

import com.biu.billiards.service.IShopService;
import com.biu.billiards.task.AsyncTask;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;


@Data
@Configuration
@ConfigurationProperties(prefix = "spring.mqtt")
public class MqttConfig {

    @Resource
    private AsyncTask asyncTask;

    public String host;

    public String username;

    public String password;

    public String clientId;

    public int timeOut;

    public int keepAlive;


    public boolean clearSession;

    public String topic1;

    public String topic2;
//
    public String topic3;

    @Bean//注入Spring
    public MyMqttClient myMqttClient() {
        MyMqttClient myMqttClient = new MyMqttClient(host, username, password, clientId, timeOut, keepAlive, clearSession,asyncTask);
        for (int i = 0; i < 10; i++) {
            try {
                myMqttClient.connect();
                // 这里可以订阅主题，推荐放到 MqttCallbackExtended.connectComplete方法中
                //myMqttClient.subscribe("ABC", 1);
                return myMqttClient;
            } catch (MqttException e) {

                System.out.println("== MqttConfig ==> MQTT connect exception, connect time = {}"+i);
               // log.error("== MqttConfig ==> MQTT connect exception, connect time = {}", i);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return myMqttClient;
    }

}
